What is is-number?
The is-number npm package is a simple utility for checking if a value is a number. It works with various types of numeric representations, including strings that can be coerced to numbers, and provides a straightforward API for determining if a value can be considered a number in JavaScript.
What are is-number's main functionalities?
Check if a value is a number
This feature allows you to check if a value is a number, including numeric strings.
const isNumber = require('is-number');
console.log(isNumber(5)); // true
console.log(isNumber('5')); // true
console.log(isNumber('five')); // false
Check if a value is not a number
This feature allows you to check if a value is not a number, including strings that cannot be coerced to numbers and other non-numeric types.
const isNumber = require('is-number');
console.log(isNumber('foo')); // false
console.log(isNumber([1, 2, 3])); // false
Check if a value is a finite number
This feature allows you to check if a value is a finite number, excluding Infinity, -Infinity, and NaN.
const isNumber = require('is-number');
console.log(isNumber(Infinity)); // false
console.log(isNumber(-Infinity)); // false
console.log(isNumber(NaN)); // false
Other packages similar to is-number
isnumeric
The isnumeric package offers similar functionality to is-number by checking if a value is numeric. It is similar in its simplicity and direct approach.
numerable
The numerable package is a more comprehensive library for number validation and formatting. It provides additional functionality compared to is-number, such as parsing and formatting numbers with localization support.
validate.io-number-primitive
This package is part of the validate.io suite and checks if a value is a number primitive. It is more strict than is-number as it does not consider numeric strings to be numbers.
is-number
Is the value a number? Has extensive tests.
Install
Install with npm:
npm i is-number --save-dev
Run tests
npm test
Usage
var isNumber = require('is-number');
true
isNumber(5e3);
isNumber(0xff);
isNumber(-1.1);
isNumber(0);
isNumber(1);
isNumber(1.1);
isNumber(10);
isNumber(10.10);
isNumber(100);
isNumber('-1.1');
isNumber('0');
isNumber('012');
isNumber('0xff');
isNumber('1');
isNumber('1.1');
isNumber('10');
isNumber('10.10');
isNumber('100');
isNumber('5e3');
isNumber(parseInt('012'));
isNumber(parseFloat('012'));
isNumber(Infinity);
isNumber('Infinity');
If you want Infinity
to be false
, just do:
var isNumber = require('is-number');
function isNum(val) {
return isNumber(val) && isFinite(val);
}
false
isNumber('3abc');
isNumber('abc');
isNumber('abc3');
isNumber('null');
isNumber('undefined');
isNumber([1, 2, 3]);
isNumber(function () {});
isNumber(new Buffer('abc'));
isNumber(null);
isNumber(undefined);
isNumber({abc: 'abc'});
isNumber({});
isNumber([]);
Notes
Instead of using isFinite()
, you can also achieve similar results by using something like ((+n+1) / (+n+1) === 1))
, but this alone allows values like null
to pass as numbers (in JavaScript, the leading +
coerces the value to a number, see this gist for some oddities).
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on September 21, 2014.